package com.huxiu.pagers;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.huxiu.R;
import com.huxiu.base.BasePager;
import com.huxiu.base.ZiXunDetailPager;
import com.huxiu.lib.PagerSlidingTab;
import com.huxiu.listener.ListenerConstans;
import com.huxiu.listener.ViewPagerListener;
import com.huxiu.pagers.qunzu.FaXian;
import com.huxiu.pagers.qunzu.MineQun;
import com.huxiu.utils.Settings;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.umeng.analytics.MobclickAgent;


public class QunZuPager extends BasePager implements ViewPagerListener {
    private PagerSlidingTab indicator;// 新闻详情里面的标题指针控件
    private ViewPager viewpager;// 新闻详情里面的ViewPager

    public QunZuPager(Context context) {
        super(context);
    }

    View view;

    @Override
    public void setCurrentItem(int page) {
        viewpager.setCurrentItem(page);
    }

    private ViewPagerListener mViewPagerListener;

    @Override
    public void setCurrentItem(int page, ViewPagerListener mViewPagerListener) {
        viewpager.setCurrentItem(page);
        this.mViewPagerListener = mViewPagerListener;//这里把获取到的对象传递进来
        this.mViewPagerListener.setCurrentItem(page);
    }

    @Override
    protected View init() {
        view = View.inflate(mContext, R.layout.newsdetail, null);
        ListenerConstans.mQunZuPager = this;//这里获取到群组对象
        return view;
    }


    @Override
    public void initData() {
        indicator = (PagerSlidingTab) view.findViewById(R.id.indicator);
        viewpager = (ViewPager) view.findViewById(R.id.pager);
        // 给ViewPager设置数据
        viewpager.setAdapter(new MyAdapter());
        // 让指针控件绑定ViewPager
        indicator.setViewPager(viewpager);
        viewpager.setCurrentItem(1);//默认选中发现页面
        if (Settings.isLoggedIn()) {
            viewpager.setCurrentItem(0);//默认选中我的页面
        }
    }

    String[] s = {"我的", "发现"};


    class MyAdapter extends PagerAdapter {
        // 返回标题
        @Override
        public CharSequence getPageTitle(int position) {
            return s[position];
        }

        @Override
        public int getCount() {
            return s.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        private ZiXunDetailPager pager;

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            switch (position) {
                case 0:
                    if (!Settings.isLoggedIn()) {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_PAGE_UNLOGIN_MY);
                    }else{
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_LOGIN, UmenEvent.GROUP_PAGE_LOGIN_MY);
                    }
                    pager = new MineQun(mContext);
                    container.addView(pager.rootView);
                    // 更新数据
                    pager.initData();
                    break;
                case 1:
                    if (!Settings.isLoggedIn()) {
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_UNLOGIN, UmenEvent.GROUP_PAGE_UNLOGIN_DISCOVER);
                    }else{
                        UmenEvent.UmengEventMap(mContext, UmenEvent.GROUP_PAGE_LOGIN, UmenEvent.GROUP_PAGE_LOGIN_DISCOVER);
                    }
                    pager = new FaXian(mContext);
                    container.addView(pager.rootView);
                    // 更新数据
                    pager.initData();
                    break;
            }
            return pager.rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }




}
